<?php
require_once '../library/core.php';

class App {
	public $table;
	public $title;
	public $fields;
	public $seo;
	
	public function save($id = null){
		if($id){
			$query="update ".$this->title." set ";
			foreach ($this->fields as $key => $value) {
				$query.= $value['name'] .'='. "'" . $value['value'] . "',"; 
			}
			
			if($this->seo){
				$query .= "meta_key = '$meta_key', ";
				$query .= "meta_des = '$meta_des', ";
			}
			
			//remove last ,
			rtrim($query, ",");	
				
			$query.=" where id=$id";	
			update($query);
		}else{
			
		}
	}
	
	public function getAll(){
		$qr = "select * from ". $this->table;
		return find($qr);
	}
	
	public function get($id){
		$qr = "select * from ". $this->table." where id = $id";
		return find($qr,'single');
	}
	
	public function query($query){
		return find($query);
	}
	
	public function createForm($data = null){
		$title = !empty($data)?'Edit ':'Add ';
		$title .= $this->title; 
		$form = '<div class="page-header">
				    <div class="pull-left">'.$title.'</div>
				    <div class="pull-right"></div>
				    <div class="clear"></div>
				</div>';
		$form .= '<form class="form-horizontal" action="" role="form" name="frmedit" method="post" enctype="multipart/form-data">';
		foreach ($this->fields as $key => $value) {
			$value['value'] = !empty($data[$value['name']])?$data[$value['name']]:"";
			$form .= createField($value);
		}
		$form .= '</form>';
		
		return $form;
	}
	
		
	
	
}
